// pages/goods_list/index.js
import {request}  from "../../request/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        name: "综合",
        isActive: true
      },
      {
        id: 1,
        name: "销量",
        isActive: false
      },
      {
        id: 2,
        name: "价格",
        isActive: false
      },
    ],
    listData: [],
  },
  QueryParams:{
    query:"",
    cid:"",
    pagenum:1,
    pagesize:10
  },
  // 总页数
  totalPages:1,
  naviClick(e){
    const {detail} =  e;
    this.setData({
      tabs: detail,
    })
  },
  getGoodsList(jerryData){
    request({
      url: '/goods/search',
      data: jerryData,
    }).then(res=>{
      if(this.QueryParams.pagenum === 1){
        const total = parseInt(res.data.message.total);
        this.totalPages = Math.ceil(total / this.QueryParams.pagesize);
      }
      let tempData = [];
      tempData = [...this.data.listData,...res.data.message.goods]
      this.setData({
        listData: tempData,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.QueryParams.cid = options.cid;
    this.getGoodsList(this.QueryParams)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.QueryParams.pagenum = 1;
    this.setData({
      listData: []
    })
    this.getGoodsList(this.QueryParams);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.QueryParams.pagenum >= this.totalPages){
      console.log('到底了')
      wx.showToast({
        title: '我也是有底线的主人',
      });
    }else{
      this.QueryParams.pagenum = this.QueryParams.pagenum + 1;
      this.getGoodsList(this.QueryParams);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {


  }
})